<template>
  <organizeManageView
    :isLoading="isLoading"
    :orgdata="orgdata"
    :defaultProps="defaultProps"
    :orgform="orgform"
    :addToporgform="addToporgform"
    :addChildorgform="addChildorgform"
    :addTopOrgdialog="addTopOrgdialog"
    :addChildOrgdialog="addChildOrgdialog"
    :isabled="isabled"
    :stationData="stationData"
    :isChildOrg="isChildOrg"
    :addStationdialog="addStationdialog"
    :addStationform="addStationform"
    :editStationdialog="editStationdialog"
    :stationLevelList="stationLevelList"
    :levelSearch="levelSearch"
    :pageSize="pageSize"
    :total="total"
    :currentPage="currentPage"
    :orgTypeList="orgTypeList"
    @orgNodeClick="orgNodeClick"
    @addTopOrgClose="addTopOrgClose"
    @addOrtCancel="addOrtCancel"
    @orgKeep="orgKeep"
    @addOrganize="addOrganize"
    @tabsClick="tabsClick"
    @addChildOrg="addChildOrg"
    @orgChildKeep="orgChildKeep"
    @addChildOrgClose="addChildOrgClose"
    @addChildOrtCancel="addChildOrtCancel"
    @editKeep="editKeep"
    @delKeep="delKeep"
    @addStation="addStation"
    @addStationClose="addStationClose"
    @addStationCancel="addStationCancel"
    @addStationKeep="addStationKeep"
    @editStation="editStation"
    @delStation="delStation"
    @editStationClose="editStationClose"
    @editStationCancel="editStationCancel"
    @editStationKeep="editStationKeep"
    @selectStation="selectStation"
    @changePage="changePage"
    @sizeChange="sizeChange"
    ref="organzieManage"
  ></organizeManageView>
</template>

<script>
  import organizeManageView from './organizeManageView'

  export default {

    components: {
      organizeManageView
      // name
    },
    name: "organizeManageLogic",
    props: {
      // msg: String //  [name: typeof]
    },
    filters: {},
    computed: {},
    watch: {},
    data() {
      return {
        isLoading: false, // 是否是加载状态
        orgdata: [], // 组织数据
        defaultProps: {
          children: 'childs',
          label: 'name'
        },
        orgform: {
          name: "",
          subname: "",
          type: "",
          phone: "",
          website: "",
          path: "",
          childPath: "",
          orclId: "",
          brief: "",
          color: ''
        },
        addToporgform: {
          name: "",
          subname: "",
          type: "",
          phone: "",
          website: "",
          path: "",
          orclId: "",
          brief: "",
          color: '#000000'
        },
        addChildorgform: {
          name: "",
          subname: "",
          type: "",
          phone: "",
          website: "",
          path: "",
          orclId: "",
          brief: "",
          color: '#000000'
        },
        addTopOrgdialog: false,
        addChildOrgdialog: false,
        isabled: true,
        treeSelectItem: {}, // 存储选择的树节点的信息
        stationData: [], // 岗位列表
        isChildOrg: true, // 判断是否是机构页
        addStationdialog: false,
        addStationform: {
          name: "",
          fullname: "",
          level: ""
        },
        editStationdialog: false, // 编辑弹窗状态
        selectStationInfo: {}, // 选中的岗位行信息
        stationLevelList: [],
        levelSearch: [], // 岗位级别差岗位
        pageSize: 10,
        total: 0,
        currentPage: 1,
        stationDataSum: [],
        orgTypeList: []
      }
    },
    created() {
      this.getOrgList();
      this.getOrgTypeList();
      this.getStationLevelList();
    },
    mounted() {

    },
    methods: {
      // 获取组织树
      getOrgList() {
        this.isLoading = true;
        _https.post.getOrgFindAll().then(res => {
          if (res.data.code == 0) {
            this.orgdata = res.data.data;
            this.isLoading = false;
          }
        })
      },
      /**
       * 点击树节点返回树信息
       * @param nodeInfo
       */
      orgNodeClick(nodeInfo) {
        this.isabled = false;
        this.orgform = {
          name: nodeInfo.name,
          subname: nodeInfo.subname,
          type: nodeInfo.type,
          phone: nodeInfo.telephone,
          website: nodeInfo.website,
          path: nodeInfo.path,
          orclId: nodeInfo.orclid,
          brief: nodeInfo.brief,
          color: nodeInfo.color
        }
        this.treeSelectItem = nodeInfo;
        this.getPosition(nodeInfo.id);
      },
      // 获取岗位级别列表
      getStationLevelList() {
        let obj = {
          datatype: "position_level"
        };
        _https.post.getDict(obj).then(res => {
          if (res.data.code == 0) {
            this.stationLevelList = res.data.data;
            let newArr = [...res.data.data];
            newArr.unshift({
              datavalue: "",
              datacode: "全部"
            });
            this.levelSearch = newArr;
          }
        })
      },
      getOrgTypeList() {
        let obj = {
          datatype: "org_type"
        }
        _https.post.getDict(obj).then(res => {
          if (res.data.code == 0) {
            this.orgTypeList = res.data.data;
          } else {
            this.$message({type: 'error', message: res.data.message});
          }
        })
      },
      // 切换tabs的回调
      tabsClick(e) {
        this.isChildOrg = e.name == 'first' ? true : false;
      },
      // 新增顶级结构
      addOrganize() {
        this.isabled = true;
        this.reset();
        this.addTopOrgdialog = true;
      },
      // 关闭新增顶级机构弹窗的回调
      addTopOrgClose() {
        this.addTopOrgdialog = false;
        this.addTopFormReset();
        this.$refs.organzieManage.$refs.addToporgform.resetFields();
      },
      // 取消新增顶级结构
      addOrtCancel() {
        this.addTopOrgdialog = false;
        // this.addTopFormReset();
        // this.$refs.organzieManage.$refs.addToporgform.resetFields();
      },
      // 新增顶级结构保存
      orgKeep() {
        let addToporgform = this.addToporgform;
        let obj = {
          name: addToporgform.name,
          subname: addToporgform.subname,
          type: addToporgform.type,
          website: addToporgform.website,
          brief: addToporgform.brief,
          path: addToporgform.path,
          telephone: addToporgform.phone,
          ocode: "",
          show: 1,
          pid: "0",
          orclid: addToporgform.orclId,
          color: addToporgform.color.substring(1)
        };


        this.$refs.organzieManage.$refs.addToporgform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            _https.post.getSaveOrg(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.getOrgList();
                this.addTopOrgdialog = false;
                // this.addTopFormReset();
                // this.$refs.organzieManage.$refs.addToporgform.resetFields();
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })
      },
      // 新增子机构
      addChildOrg() {
        this.isabled = true;
        this.addChildOrgdialog = true;
      },
      // 保存新增子机构
      orgChildKeep() {
        let addChildorgform = this.addChildorgform;
        let obj = {
          name: addChildorgform.name,
          subname: addChildorgform.subname,
          type: addChildorgform.type,
          website: addChildorgform.website,
          brief: addChildorgform.brief,
          path: `${this.orgform.path}.${addChildorgform.path}`,
          telephone: addChildorgform.phone,
          ocode: "",
          show: 1,
          pid: this.treeSelectItem.id,
          orclid: addChildorgform.orclId,
          color: addChildorgform.color.substring(1)
        };


        this.$refs.organzieManage.$refs.addChildorgform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            _https.post.getSaveOrg(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.getOrgList();
                this.addChildOrgdialog = false;
                // this.addChildFormReset();
                // this.$refs.organzieManage.$refs.addChildorgform.resetFields();
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })


      },
      // 关闭新增子机构弹窗的回调
      addChildOrgClose() {
        this.addChildOrgdialog = false;
        this.addChildFormReset();
        this.$refs.organzieManage.$refs.addChildorgform.resetFields();
      },
      // 取消新增子机构
      addChildOrtCancel() {
        this.addChildOrgdialog = false;
        // this.addChildFormReset();
        // this.$refs.organzieManage.$refs.addChildorgform.resetFields();
      },
      // 编辑保存
      editKeep() {
        let orgform = this.orgform;
        let treeSelectItem = this.treeSelectItem;
        let obj = {
          id: treeSelectItem.id,
          subname: orgform.subname,
          type: orgform.type,
          name: orgform.name,
          website: orgform.website,
          brief: orgform.brief,
          path: orgform.path,
          telephone: orgform.phone,
          color: orgform.color.substring(1),
          ocode: treeSelectItem.ocode,
          show: treeSelectItem.show,
          orclid: orgform.orclId
        };

        this.$refs.organzieManage.$refs.editorgform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            _https.post.getUpdateOrg(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.getOrgList();
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })
      },
      // 删除机构
      delKeep() {
        let obj = {
          id: this.treeSelectItem.id
        };
        this.$confirm('此操作将永久删除该机构, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _https.post.getDeleteOrg(obj).then(res => {
            if (res.data.code == 0) {
              this.$message({type: 'success', message: res.data.message});
              this.getOrgList();
              this.isabled = true;
              this.reset();
              this.$refs.organzieManage.$refs.editorgform.resetFields();
            } else {
              this.$message({type: 'error', message: res.data.message});
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      // 新增岗位
      addStation() {
        this.addStationdialog = true;
      },
      // 关闭新增岗位弹窗的回调
      addStationClose() {
        this.addStationdialog = false;
        this.addStationReset();
        this.$refs.organzieManage.$refs.addStationform.resetFields();
      },
      // 取消新增岗位
      addStationCancel() {
        this.addStationdialog = false;
        // this.addStationReset();
        // this.$refs.organzieManage.$refs.addStationform.resetFields();
      },
      // 保存新增岗位
      addStationKeep() {
        let addStationform = this.addStationform;
        let obj = {
          active: 1,
          fullname: `${this.orgform.name}${addStationform.fullname}`,
          name: addStationform.name,
          ord: 0,
          level: addStationform.level,
          pid: "0",
          orgid: this.treeSelectItem.id
        };


        this.$refs.organzieManage.$refs.addStationform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            _https.post.getSavePosition(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.getPosition(this.treeSelectItem.id);
                this.addStationdialog = false;
                // this.addStationReset();
                // this.$refs.organzieManage.$refs.addStationform.resetFields();
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })
      },
      // 编辑岗位
      editStation(index, row) {
        this.editStationdialog = true;
        let fullname = row.fullname.substring(row.fullname.indexOf(row.name));
        this.addStationform = {
          name: row.name,
          fullname: fullname,
          level: row.level
        };
        this.selectStationInfo = row;
      },
      // 关闭编辑岗位弹窗的回调
      editStationClose() {
        this.editStationdialog = false;
        this.addStationReset();
        this.$refs.organzieManage.$refs.editStationform.resetFields();
      },
      // 取消编辑岗位
      editStationCancel() {
        this.editStationdialog = false;
        // this.addStationReset();
        // this.$refs.organzieManage.$refs.editStationform.resetFields();
      },
      // 编辑岗位保存
      editStationKeep() {
        let addStationform = this.addStationform;
        let obj = {
          active: 1,
          fullname: `${this.orgform.name}${addStationform.fullname}`,
          name: addStationform.name,
          ord: 0,
          level: addStationform.level,
          pid: "0",
          orgid: this.treeSelectItem.id,
          id: this.selectStationInfo.id
        };


        this.$refs.organzieManage.$refs.editStationform.validate((valid) => {
          if (!valid) {
            return;
          } else {
            _https.post.getUpdatePosition(obj).then(res => {
              if (res.data.code == 0) {
                this.$message({type: 'success', message: res.data.message});
                this.editStationdialog = false;
                this.getPosition(this.treeSelectItem.id);
                // this.addStationReset();
                // this.$refs.organzieManage.$refs.editStationform.resetFields();
              } else {
                this.$message({type: 'error', message: res.data.message});
              }
            })
          }
        })
      },
      // 删除岗位
      delStation(index, row) {
        let obj = {
          id: row.id
        };

        this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _https.post.getDeletePosttion(obj).then(res => {
            if (res.data.code == 0) {
              this.$message({type: 'success', message: res.data.message});
              this.getPosition(this.treeSelectItem.id);
            } else {
              this.$message({type: 'error', message: res.data.message});
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });


      },
      // 根据id获取岗位列表
      getPosition(orgid) {
        let obj = {orgid: orgid};
        _https.post.getAllPosition(obj).then(res => {
          if (res.data.code == 0) {
            let newContent = [...res.data.data];
            let stationLevelList = this.stationLevelList;
            let newArr = newContent.map((item) => {
              for (let i = 0; i < stationLevelList.length; i++) {
                if (item.level == stationLevelList[i].datavalue) {
                  item.levelCode = stationLevelList[i].datacode;
                  break;
                }
              }
              return item;
            });

            this.stationDataSum = newArr;
            let currentPage = this.currentPage;
            if (newArr.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(newArr.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = newArr.length;
          }
        })
      },
      /**
       * 根据层级查岗位
       * @params item 选中值
       */
      selectStation(item) {
        // this.treeSelectItem
        let obj = {
          orgid: this.treeSelectItem.id,
          level: item
        }
        _https.post.getAllPosition(obj).then(res => {
          if (res.data.code == 0) {
            let newContent = [...res.data.data];
            let stationLevelList = this.stationLevelList;
            let newArr = newContent.map((item) => {
              for (let i = 0; i < stationLevelList.length; i++) {
                if (item.level == stationLevelList[i].datavalue) {
                  item.levelCode = stationLevelList[i].datacode;
                  break;
                }
              }
              return item;
            });

            this.stationDataSum = newArr;
            let currentPage = this.currentPage;
            if (newArr.length % this.pageSize == 0 && currentPage != 1) {
              currentPage = Math.ceil(newArr.length / this.pageSize);
              this.currentPage = currentPage;
            }
            this.setDataList(currentPage);
            this.total = newArr.length;

          }
        })
      },
      changePage(page) {
        this.currentPage = page;
        this.setDataList(page);
      },
      sizeChange(num) {
        this.pageSize = num;
        this.getPosition(this.treeSelectItem.id);
      },
      /**
       * 将返回的数据进行分页操作
       * @param currentPage {Number}
       */
      setDataList(currentPage) {
        let current = currentPage * this.pageSize;
        let currentStart = current - this.pageSize;
        let currentEnd = current;
        this.stationData = this.stationDataSum.slice(currentStart, currentEnd);
      },
      // 初始化
      reset() {
        this.orgform = {
          name: "",
          subname: "",
          type: "",
          phone: "",
          website: "",
          path: "",
          orclId: "",
          brief: "",
          color: ''
        }
      },
      addTopFormReset() {
        this.addToporgform = {
          name: "",
          subname: "",
          type: "",
          phone: "",
          website: "",
          path: "",
          orclId: "",
          brief: "",
          color: '#000000'
        }
      },
      addChildFormReset() {
        this.addChildorgform = {
          name: "",
          subname: "",
          type: "",
          phone: "",
          website: "",
          path: "",
          orclId: "",
          brief: "",
          color: '#000000'
        }
      },
      addStationReset() {
        this.addStationform = {
          name: "",
          fullname: "",
          level: ""
        }
      }
    },
    destroyed() {

    }
  }
</script>

<style scoped lang="less">

</style>
